---@class BP_Tile_C:AActor
---@field Pos USceneComponent
---@field Cylinder UStaticMeshComponent
---@field DefaultSceneRoot USceneComponent
---@field IsDestroyed bool
---@field ID int32
---@field Left ABP_Tile_C
---@field Right ABP_Tile_C
---@field LeftUp ABP_Tile_C
---@field LeftDown ABP_Tile_C
---@field RightUp ABP_Tile_C
---@field RightDown ABP_Tile_C
---@field Level int32
--Edit Below--
require("Script.Common.Debug")

local BP_Tile = {
    --待补充
    
    MiddleLocation = nil;
};

function BP_Tile:ReceiveBeginPlay()
    self.SuperClass.ReceiveBeginPlay(self);
    -- self.Left.OnActorHit:Add(self.Left_OnActorHit,self)
    -- print("BP_Tile:ReceiveBeginPlay");
    -- self.MiddleLocation = self:K2_GetActorLocation()

    -- ugcprint("Tile:ReceiveBeginPlay");
    --[[
    self.MiddleLocation = VectorHelper.ToLuaTable(self.Pos:K2_GetComponentLocation());
    self.LeftTopLocation = VectorHelper.ToLuaTable(self.PosLeftTop:K2_GetComponentLocation());
    self.RightTopLocation = VectorHelper.ToLuaTable(self.PosRightTop:K2_GetComponentLocation());
    self.LeftBottomLocation = VectorHelper.ToLuaTable(self.PosLeftBottom:K2_GetComponentLocation());
    self.RightBottomLocation = VectorHelper.ToLuaTable(self.PosRightBottom:K2_GetComponentLocation());
    ]]--
    
end

--[[
function BP_Tile:GetOnePlayerLocation()
    return self.Pos:K2_GetComponentLocation()
end

function BP_Tile:GetTwoPlayerTileLocation(index)
    local LeftSideMiddlePointLocation = VectorHelper.GetMiddlePoint(self.LeftTopLocation,self.LeftBottomLocation);
    local RightSideMiddlePointLocation = VectorHelper.GetMiddlePoint(self.RightTopLocation,self.RightBottomLocation);
    if index == 1 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,LeftSideMiddlePointLocation);
    elseif index == 2 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,RightSideMiddlePointLocation);
    end
end

function BP_Tile:GetThreePlayerTileLocation(index)
    local BottomSideMiddlePointLocation = VectorHelper.GetMiddlePoint(self.LeftBottomLocation,self.RightBottomLocation);
    if index == 1 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,self.LeftTopLocation);
    elseif index == 2 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,self.RightTopLocation);
    elseif index == 3 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,BottomSideMiddlePointLocation);
    end
end

function BP_Tile:GetFourPlayerTileLocation(index)
    local BottomSideMiddlePointLocation = VectorHelper.GetMiddlePoint(self.LeftBottomLocation,self.RightBottomLocation);
    if index == 1 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,self.LeftTopLocation);
    elseif index == 2 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,self.RightTopLocation);
    elseif index == 3 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,self.LeftBottomLocation);
    elseif index == 4 then
        return VectorHelper.GetMiddlePoint(self.MiddleLocation,self.RightBottomLocation);
    end
end
]]--

--[[
function BP_Tile:ReceiveTick(DeltaTime)
    self.SuperClass.ReceiveTick(self, DeltaTime);
end
--]]

function BP_Tile:DestroyTile()
	print("BP_Tile:DestroyTile")
    self:K2_DestroyActor()--真的可以destroy掉吗?但却实是运行了的
    print("BP_Tile:DestroyTile Finished")
end


function BP_Tile:ReceiveEndPlay()
    self.SuperClass.ReceiveEndPlay(self); 
    print("BP_Tile:ReceiveEndPlay");
end

function BP_Tile:GetPlayerOnThisTile( ... )
    print("BP_Tile:GetPlayerOnThisTile")
    local result = {}
    for _, PlayerInfo in pairs(UGCGameSystem.GameState.PlayerInfos) do
        if PlayerInfo.PlayerPawn ~= nil and PlayerInfo.PlayerPawn.CurrentTileIndex == self.ID then
            table.insert(result,PlayerInfo.PlayerPawn)
        end
    end
    if #result == 0 then
        return nil
    end
    return result
end

-- On Touch and On Clicked
function BP_Tile:ReceiveActorOnInputTouchBegin(ETouchIndex)
	print("BP_Tile:ReceiveActorOnInputTouchBegin")
    self.SuperClass.ReceiveActorOnInputTouchBegin(self,ETouchIndex)
    -- only for test
    local location = VectorHelper.ToLuaTable(self.Pos:K2_GetComponentLocation())
    
    -- 高亮显示
    self:SetTileHighlight(true);

    print("BP_Tile:ReceiveActorOnInputTouchBegin set TileChoosed")

    local PlayerController = GameplayStatics.GetPlayerController(UGCGameSystem.GameState, 0);
    if PlayerController ~= nil then
        local teamIndex = PlayerController.PlayerState.TeamIndex;
        print(teamIndex)
        -- 如果操作者即当前回合的玩家
        if teamIndex ~= nil then
            Debug:Log("BP_Tile:ReceiveActorOnInputTouchBegin", KismetSystemLibrary.GetDisplayName(self))
            UGCGameSystem.GameState.PlayerInfos[teamIndex].TileChoosed = self;
                
            -- TODO：如果用户点击确认之前没有选择卡牌
            print("BP_Tile:ReceiveActorOnInputTouchBegin. teamIndex  "..teamIndex.."  ")
        else
            print("BP_Tile:ReceiveActorOnInputTouchBegin get TeamIndex is not current teamIndex");
        end
    else
        print("BP_Tile:ReceiveActorOnInputTouchBegin get playerController failed");
    end


    
    --UGCGameSystem.GameState.MoveDir, UGCGameSystem.GameState.targetTile = CardControl.PlayerChoseTile(self)
end

-- 松开鼠标
function BP_Tile:ReceiveActorOnInputTouchEnd(ETouchIndex)
    print("BP_Tile:ReceiveActorOnInputTouchEnd")
    self.SuperClass.ReceiveActorOnInputTouchBegin(self,ETouchIndex)
    self:SetTileHighlight(false);
end

-- 高亮此地砖,已实现功能
function BP_Tile:SetTileHighlight(bHightlight)
    ugcprint("Tile:SetTileHighlight "..tostring(bHightlight));
    local highlight = UE.LoadObject(HighLightMaterials)
    local normal = UE.LoadObject(NormalMaterials)
    if bHightlight then
        self.Cylinder:SetMaterial(0,highlight)
    else
        self.Cylinder:SetMaterial(0,normal)
    end
end

function BP_Tile:Left_OnActorHit(SelfActor, OtherActor, NormalImpulse, Hit)
end
return BP_Tile;